<?php

    // +----------------------------------------------------------------------
    // | 广告信息获取
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\event\cms;

    use app\model\cms\Advertising as AdvertisingModel;
    use app\model\cms\AdvertisingSpace as AdvertisingSpaceModel;
	use app\common\event\Base;
	use app\model\ucenter\User as UserModel;
	use app\event\ucenter\User as UserEvent;
	use think\Exception;
	use think\facade\Lang;

	class Advertising extends Base
    {

        public static function lists($advertising_space_id)
        {
            return AdvertisingModel::with('image')->where('advertising_space_id', $advertising_space_id)
                ->where('status', 1)
                ->where('is_effect', 1)
                ->order('sort asc')
                ->whereTime('start_time', '<', 'now')
                ->whereTime('end_time', '>', 'now')
                ->select();
        }

        /**
         * 获取广告信息
         * @param $tag
         * @param $limit
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2019/8/3 19:40
         */
        public function get($tag, $limit = null)
        {
            try {
                if (is_numeric($tag)) {
                    $space = AdvertisingSpaceModel::where('id', $tag)->where('status', 1)->find();
                } else {
                    $space = AdvertisingSpaceModel::where('name', $tag)->where('status', 1)->find();
                }
                if (!$space) {
                    throw new Exception('广告位不存在');
                }
                if (is_null($limit)) {
                    $limit = $space['limit'];
                }
                if (1 == $limit) {
                    $banners = AdvertisingModel::where('advertising_space_id', $space['id'])
                        ->whereTime('start_time', '<', time_format())
                        ->whereTime('end_time', '>', time_format())
                        ->where('status', 1)
                        ->order('sort ASC')->find();
                } elseif (0 == $limit) {
                    $banners = AdvertisingModel::where('advertising_space_id', $space['id'])
                        ->whereTime('start_time', '<', time_format())
                        ->whereTime('end_time', '>', time_format())
                        ->where('status', 1)
                        ->order('sort ASC')->select();
                } else {
                    $banners = AdvertisingModel::where('advertising_space_id', $space['id'])
                        ->whereTime('start_time', '<', time_format())
                        ->whereTime('end_time', '>', time_format())
                        ->where('status', 1)
                        ->limit($limit)
                        ->order('sort ASC')->select();
                }
                $data['code'] = 0;
                $data['limit'] = $limit;
                $data['data'] = $banners;
            } catch (\Exception $e) {
                $data['code'] = 2;
                $data['message'] = $e->getMessage();
            }
            return $data;
        }

		/**
		 * 通用更新方法
		 * @param array   $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/6/30 17:31
		 */
		public function update($param, $user = null)
		{
			try {
				if (empty($param)) {
					throw new Exception(Lang::get('__data_format_error_'));
				}
				if (!empty($param['id'])) {
					if (!UserEvent::isAdministrator($user))
					{
						$store = $user->store()->find();
						if (!$store)
						{
							throw new Exception('请先开通商铺功能');
						}
						$model = AdvertisingModel::where('id',$param['id'])->where('store_id',$store['id'])->find();
					}else{
						$model = AdvertisingModel::where('id',$param['id'])->find();
//						$model['is_effect'] = $param['is_effect'];
					}
					if (!$model)
					{
						throw new Exception('要操作的广告不存在');
					}
					unset($param['id']);
					// 更新操作
					$result = $model->save($param);
				} else { // 新增
					if (UserEvent::isAdministrator($user))
					{
						$param['is_effect'] = 1;
					}else{
						$store = $user->store()->find();
						if (!$store)
						{
							throw new Exception('请先开通商铺功能');
						}
						$param['store_id'] = $store['id'];
//						$param['is_effect'] = 0;
					}
					$result = AdvertisingModel::create($param);
				}
				$data['code'] = 0;
				$data['data'] = $result;
				$data['message'] = '操作成功';
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

	}